%%%-------------------------------------------------------------------
%%% @author Viktor Green <viktor@student.gu.se>
%%% @copyright (C) 2011, Viktor Green
%%% @doc
%%% Test for the module torrent_handler.erl
%%% @end
%%% Created : 12 Dec 2011 by Viktor Green <viktor@student.gu.se>
%%%-------------------------------------------------------------------
-module(torrent_handler_tests).

-include_lib("eunit/include/eunit.hrl").

torrent_handler_test_() ->
    {foreach, fun setup/0, fun teardown/1,
     [{with, [Test]} || Test <- [
                                 fun add_torrent/1,
                                 fun handle/1
				 ]]}.


setup() ->
    application:start(snoopy_torrent).

teardown(_) ->
    application:stop(snoopy_torrent).


add_torrent(_) ->
    TorrentName = "Enarmad bandit.txt",
    {ok, Dir} = file:get_cwd(),
    Parent = filename:dirname(Dir),
    Path = filename:join([Parent, priv, "Test.torrent"]),
    {added, _ID, Type, Name, Status} = torrent_handler:add_torrent(Path),
    ?assert(Type == file),
    ?assert(Name == TorrentName),
    ?assert(Status == "added").

handle(_) ->
    ?assert(gen_server:call(torrent_handler, call_test) == ok),
    ?assert(gen_server:cast(torrent_handler, cast_test) == ok),
    PidA = whereis(torrent_handler),
    PidA ! info_test,
    PidB = whereis(torrent_handler),
    ?assert(PidA == PidB),
    Ref = make_ref(),
    ?assert([] == torrent_handler:get_peers(Ref)).

